package com.zhxy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhxy.mapper.StudentMapper;
import com.zhxy.pojo.Student;
import com.zhxy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.sql.SQLException;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private BCryptPasswordEncoder bcryptPasswordEncoder;
    @Override
    public Page<Student> selectByPage(Long pageNum, Long pageSize, Student student) throws Exception {
        Page<Student> page=new Page<>(pageNum,pageSize);
        return studentMapper.selectStudentListByPage(page, student);
    }

    @Override
    public int addStudent(Student student) throws SQLException {

        //先进行密码加密
       if(!StringUtils.isEmpty(student.getPassword()))
               student.setPassword(bcryptPasswordEncoder.encode(student.getPassword()));
        return studentMapper.insert(student);
    }

    @Override
    public int update(Student student) throws SQLException {
        //先进行密码加密
        if(!StringUtils.isEmpty(student.getPassword()))
            student.setPassword(bcryptPasswordEncoder.encode(student.getPassword()));
        return studentMapper.updateById(student);
    }
}
